/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.mailbox.ContactConstants;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.GalContact;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.gal.GalSearchControl;
/*     */ import com.zimbra.cs.gal.GalSearchParams;
/*     */ import com.zimbra.cs.gal.GalSearchResultCallback;
/*     */ import com.zimbra.cs.servlet.util.JettyUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.type.GalSearchType;
/*     */ import java.util.Map;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SyncGal
/*     */   extends GalDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  52 */     disableJettyTimeout(context);
/*  53 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  54 */     Account account = getRequestedAccount(getZimbraSoapContext(context));
/*     */     
/*  56 */     if (!canAccessAccount(zsc, account)) {
/*  57 */       throw ServiceException.PERM_DENIED("can not access account");
/*     */     }
/*  59 */     String tokenAttr = request.getAttribute("token", "");
/*  60 */     String galAcctId = request.getAttribute("galAcctId", null);
/*  61 */     boolean idOnly = request.getAttributeBool("idOnly", false);
/*     */     
/*  63 */     GalSearchParams params = new GalSearchParams(account, zsc);
/*  64 */     params.setType(GalSearchType.all);
/*  65 */     params.setToken(tokenAttr);
/*  66 */     params.setRequest(request);
/*  67 */     params.setResponseName(AccountConstants.SYNC_GAL_RESPONSE);
/*  68 */     params.setIdOnly(idOnly);
/*  69 */     params.setUserAgent(zsc.getUserAgent());
/*  70 */     if (galAcctId != null)
/*  71 */       params.setGalSyncAccount(Provisioning.getInstance().getAccountById(galAcctId));
/*  72 */     params.setResultCallback(new SyncGalCallback(params, null));
/*     */     
/*  74 */     GalSearchControl gal = new GalSearchControl(params);
/*  75 */     gal.sync();
/*  76 */     return params.getResultCallback().getResponse();
/*     */   }
/*     */   
/*     */   public boolean needsAuth(Map<String, Object> context)
/*     */   {
/*  81 */     return true;
/*     */   }
/*     */   
/*     */   private static class SyncGalCallback
/*     */     extends GalSearchResultCallback
/*     */   {
/*     */     private static final String UA_ZCO = "ZimbraConnectorForOutlook";
/*     */     private static final String UA_ZCB = "ZimbraConnectorForBES";
/*     */     boolean mNeedPreHelixCompatibility;
/*     */     
/*     */     private SyncGalCallback(GalSearchParams params)
/*     */     {
/*  93 */       super();
/*     */       
/*  95 */       ZimbraSoapContext zsc = params.getSoapContext();
/*  96 */       if (zsc != null) {
/*  97 */         String ua = zsc.getUserAgent();
/*     */         
/*     */ 
/*     */ 
/*     */ 
/* 102 */         if (ua != null) {
/* 103 */           String[] parts = ua.split("/");
/* 104 */           if (parts.length == 2) {
/* 105 */             String app = parts[0];
/* 106 */             String version = parts[1];
/*     */             
/* 108 */             if (("ZimbraConnectorForOutlook".equalsIgnoreCase(app)) || ("ZimbraConnectorForBES".equalsIgnoreCase(app))) {
/* 109 */               String[] release = version.split("\\.");
/* 110 */               if (release.length >= 1) {
/*     */                 try {
/* 112 */                   int major = Integer.parseInt(release[0]);
/* 113 */                   if (major < 7)
/* 114 */                     this.mNeedPreHelixCompatibility = true;
/*     */                 } catch (NumberFormatException e) {
/* 116 */                   ZimbraLog.gal.debug("unable to parse user agent version " + version, e);
/*     */                 }
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     public void handleContact(GalContact c)
/*     */       throws ServiceException
/*     */     {
/* 136 */       if ((this.mNeedPreHelixCompatibility) && (c.isGroup())) {
/* 137 */         boolean isZimbraGroup = c.getSingleAttr("zimbraId") != null;
/* 138 */         if (isZimbraGroup) {
/* 139 */           Map<String, Object> attrs = c.getAttrs();
/* 140 */           Object member = attrs.get(ContactConstants.A_member);
/* 141 */           Object mailForwardingAddress = attrs.get("zimbraMailForwardingAddress");
/* 142 */           if ((member != null) && (mailForwardingAddress == null)) {
/* 143 */             attrs.put("zimbraMailForwardingAddress", member);
/* 144 */             attrs.remove(ContactConstants.A_member);
/*     */           }
/*     */         }
/*     */       }
/* 148 */       super.handleContact(c);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void disableJettyTimeout(Map<String, Object> context)
/*     */   {
/* 170 */     if (LC.zimbra_gal_sync_disable_timeout.booleanValue()) {
/* 171 */       Object request = context.get("servlet.request");
/* 172 */       if ((request instanceof HttpServletRequest)) {
/* 173 */         JettyUtil.setIdleTimeout(0L, (HttpServletRequest)request);
/*     */       } else {
/* 175 */         ZimbraLog.misc.warn("no request in context map, cannot disable timeout");
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/SyncGal.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */